package lex.mtd.core.net;

import java.net.URL;
import java.net.URLConnection;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.lex.net.ftp.FTPURL;
import org.lex.net.ftp.IFTPClient;
import org.lex.net.ftp.util.FTPConnector;
import org.lex.net.log.Log;


public class Connector {
	/**
	 * ����<code>url</code>�����ӳɹ���֪ͨ<code>callback</code>
	 * 
	 * @param url
	 * @param log
	 * @param callback
	 */
	public static void connectFTP( FTPURL url, final Log log, final ChangeListener callback )
	{
		final FTPConnector connector = new FTPConnector( log );
		connector.addChangeListener( new ChangeListener(){
			@Override
			public void stateChanged( ChangeEvent e )
			{
				IFTPClient client = null;
				if( true == connector.isConnected() )
				{
					client = connector.getFtpClient();
					callback.stateChanged( new ChangeEvent( client ) );
				}
			}
		} );
		connector.connect( url );
	}

	public static void connectHTTP( final URL url, final long offset, final Log log,
		final ChangeListener callback )
	{
		final SunHttpConnector connector = new SunHttpConnector( log );
		connector.addChangeListener( new ChangeListener(){
			@Override
			public void stateChanged( ChangeEvent e )
			{
				URLConnection client = null;
				if( true == connector.isConnected() )
				{
					client = connector.getConnection();
					callback.stateChanged( new ChangeEvent( client ) );
				}
			}
		} );
		connector.connect( url );
	}
}
